package com.jgame.secondarymarket.infrastructure.common.model.enums;

import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Getter
public enum Role {

    ADMIN("ROLE_ADMIN", 0),
    USER("ROLE_USER", 1);

    private String role;
    private Integer value;

    Role(String label, Integer value) {
        this.role = label;
        this.value = value;
    }

    /**
     * 根据逗号风格的value字符串获取角色
     *
     * @return
     */
    public static List<Role> getLabelsByValueStr(String values) {
        Map<Integer, Role> map = Arrays.stream(Role.values()).collect(Collectors.toMap(x -> x.getValue(), x -> x));

        return Arrays.asList(values.split(",")).stream().collect(ArrayList::new, (acc, t) -> {
            if (map.containsKey(Integer.parseInt(t))) {
                acc.add(map.get(Integer.parseInt(t)));
            }
        }, ArrayList::addAll);
    }


    /**
     * 角色列表转为，风格的字符串
     *
     * @param roles
     * @return
     */
    public static String getValueStr(List<Role> roles) {
        return roles.stream().collect(ArrayList<String>::new, (acc, t) -> {
            acc.add(String.valueOf(t.getValue()));
        }, ArrayList::addAll).stream().collect(Collectors.joining(","));
    }
}
